#include <math.h>
#include "vec3f.h"
using namespace std;

Vec3f::Vec3f()
{
	
}

Vec3f::Vec3f(GLfloat x, GLfloat y, GLfloat z)
{
	v[0] = x;
	v[1] = y;
	v[2] = z;
}

GLfloat &Vec3f::operator[](GLint index)
{
	return v[index];
}

GLfloat Vec3f::operator[](GLint index) const
{
	return v[index];
}

Vec3f Vec3f::operator*(GLfloat scale) const
{
	return Vec3f(v[0] * scale, v[1] * scale, v[2] * scale);
}

Vec3f Vec3f::operator/(GLfloat scale) const
{
	return Vec3f(v[0] / scale, v[1] / scale, v[2] / scale);
}

Vec3f Vec3f::operator+(const Vec3f &other) const
{
	return Vec3f(v[0] + other.v[0], v[1] + other.v[1], v[2] + other.v[2]);
}

Vec3f Vec3f::operator-(const Vec3f &other) const
{
	return Vec3f(v[0] - other.v[0], v[1] - other.v[1], v[2] - other.v[2]);
}

Vec3f Vec3f::operator-() const
{
	return Vec3f(-v[0], -v[1], -v[2]);
}

const Vec3f &Vec3f::operator*=(GLfloat scale)
{
	v[0] *= scale;
	v[1] *= scale;
	v[2] *= scale;
	return *this;
}

const Vec3f &Vec3f::operator/=(GLfloat scale)
{
	v[0] /= scale;
	v[1] /= scale;
	v[2] /= scale;
	return *this;
}

const Vec3f &Vec3f::operator+=(const Vec3f &other)
{
	v[0] += other.v[0];
	v[1] += other.v[1];
	v[2] += other.v[2];
	return *this;
}

const Vec3f &Vec3f::operator-=(const Vec3f &other)
{
	v[0] -= other.v[0];
	v[1] -= other.v[1];
	v[2] -= other.v[2];
	return *this;
}

GLfloat Vec3f::magnitude() const
{
	return sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}

GLfloat Vec3f::magnitudeSquared() const
{
	return v[0] * v[0] + v[1] * v[1] + v[2] * v[2];
}

Vec3f Vec3f::normalize() const
{
	GLfloat m = sqrt(v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
	return Vec3f(v[0] / m, v[1] / m, v[2] / m);
}

GLfloat Vec3f::dot(const Vec3f &other) const
{
	return v[0] * other.v[0] + v[1] * other.v[1] + v[2] * other.v[2];
}

Vec3f Vec3f::cross(const Vec3f &other) const
{
	return Vec3f(v[1] * other.v[2] - v[2] * other.v[1],
				 v[2] * other.v[0] - v[0] * other.v[2],
				 v[0] * other.v[1] - v[1] * other.v[0]);
}

Vec3f operator*(GLfloat scale, const Vec3f &v)
{
	return v * scale;
}

ostream &operator<<(ostream &output, const Vec3f &v)
{
	cout << '(' << v[0] << ", " << v[1] << ", " << v[2] << ')';
	return output;
}









